# -*- coding: utf-8 -*-
from abc import ABC

from app.views import CommonHandler
from app.models.dao import db_session, db2vectors, delete, add
from app.models.model import DeviceModel


class MonitorHandler(CommonHandler, ABC):
    async def get(self):
        meta_id = self.get_argument("meta_id", None)
        monitors = db2vectors(DeviceModel, meta_id)
        await self.response(data=monitors)

    async def post(self):
        params, session = self.json_params(), db_session()
        monitor = DeviceModel(params['point'], params['meta_id'])
        monitor.insert_id = str(params['node_id'])
        add(monitor)
        monitors = db2vectors(DeviceModel, params['meta_id'])
        await self.response(data=monitors)

    async def delete(self):
        params = self.json_params()
        delete(DeviceModel, params['id'])
        warnings = db2vectors(DeviceModel, params['meta_id'])
        await self.response(data=warnings)
